var zuocedaohuang = document.querySelectorAll("#zuocedaohuang>ul>li>div");
//物联设备
$("#bs-example-navbar-collapse-1 ul:nth-child(3) li:nth-child(2) a").click(function () {
    $("body").load("WuJiaHanWuLianSheBei.html");
})
//主页
$("#zuocedaohuang>ul>li:nth-child(1)").click(function () {
    $("body").load("index.html");
})
//修改密码
$("#zuocedaohuang>ul>li:nth-child(8)>ul>li:first-child").click(function () {
    $("body").load("yaoketongxiugaimm.html");
})
$(".dropdown-menu>li:first-child").click(function () {
    $("body").load("yaoketongxiugaimm.html");
})
//人员管理
$("#zuocedaohuang>ul>li:nth-child(2)>ul>li").click(function () {
    $("body").load("WuJiaHanRenYuanGuanLi.html");
})
//初始化
$("#zuocedaohuang>ul>li:nth-child(8)>ul>li:last-child").click(function () {
    $("body").load("chushihua.html");
})
//主页
$("#bs-example-navbar-collapse-1 ul:nth-child(3) li:nth-child(1) a").click(function () {
    $("body").load("index.html");
})

//物业管理
$("#zuocedaohuang>ul>li:nth-child(5)>ul>li:first-child").click(function () {
    $("body").load("HeLuYiJiaoFeiJiLu.html");
})
//订单管理
$("#zuocedaohuang>ul>li:nth-child(5)>ul>li:nth-child(2)").click(function () {
    $("body").load("HeLuYiDaiJiaoDingDan.html");
})
//收款账号
$("#zuocedaohuang>ul>li:nth-child(5)>ul>li:nth-child(3)").click(function () {
    $("body").load("HeLuYiShouKuanZhangHao.html");
})

//房源管理
$("#zuocedaohuang>ul>li:nth-child(3)>ul>li:first-child").click(function () {
    $("body").load("renchengdongfangyuanhuanli.html");
})
//房屋档案
$("#zuocedaohuang>ul>li:nth-child(3)>ul>li:nth-child(2)").click(function () {
    $("body").load("renchengdongfangwudangan.html");
})

//车位管理
$("#zuocedaohuang>ul>li:nth-child(4)>ul>li:first-child").click(function () {
    $("body").load("cx-chewguanli.html");
})
//车辆管理
$("#zuocedaohuang>ul>li:nth-child(4)>ul>li:nth-child(2)").click(function () {
    $("body").load("cx-chelguanli.html");
})

//工单配置
$("#zuocedaohuang>ul>li:nth-child(6)>ul>li:first-child").click(function () {
    $("body").load("第一个页面.html");
})
//报修管理
$("#zuocedaohuang>ul>li:nth-child(6)>ul>li:nth-child(2)").click(function () {
    $("body").load("第二个页面.html");
})
//物业信息
$("#zuocedaohuang>ul>li:nth-child(7)>ul>li:nth-child(3)").click(function () {
    $("body").load("YaoKeTongWuYeXingXi.html");
})
//信息服务
$("#zuocedaohuang>ul>li:nth-child(7)>ul>li:first-child").click(function () {
    $("body").load("LiuXueQiXinXiFuWu.html");
})
//账单配置
$("#zuocedaohuang>ul>li:nth-child(7)>ul>li:nth-child(2)").click(function () {
    $("body").load("LiuXueQiZhangDanPeiZhi.html");
})

$(".dropdown-toggle").click(function () {
    if (navpanding==1){
        $(".dropdown-menu").css("display","block");
        $(".dropdown-toggle").css("background-color","rgb(221,221,221)")
        navpanding=2;
    }else {
        $(".dropdown-menu").css("display","none");
        $(".dropdown-toggle").css("background-color","rgb(65,92,133)")
        navpanding=1;
    }
})

for(var i = 0 ;i<zuocedaohuang.length;i++){
    zuocedaohuang[i].onclick=function () {
        var liClick = this.parentNode;
        var haizi=liClick.querySelectorAll("ul>li");
        var liHeight = liClick.style.height;
        for(var n = 1 ;n<zuocedaohuang.length;n++){
            var fa=[];
            fa[n]= zuocedaohuang[n].parentNode;
            fa[n].style.height="50px";
            fa[n].style.transition="all 0.2s";
            zuocedaohuang[n].style.color="black";
            zuocedaohuang[n].querySelector("span").style.transform="rotatez(0deg)";
        }
        if(liHeight=="50px"){
            if(haizi&&haizi.length>0){
                this.style.color="black";
                liClick.style.height=50+haizi.length*50+"px";
                this.querySelector("span").style.transform="rotatez(180deg)";
                liClick.style.transition="all 0.2s";
            }
        }else {
            this.style.color="black";
            liClick.style.height="50px";
            this.querySelector("span").style.transform="rotatez(0deg)";
            liClick.style.transition="all 0.2s";
            this.querySelector("span").style.transform="rotatez(0deg)";
        }
    }
}
$(window).scroll(function () {
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var i=scrollTop;
    if (scrollTop>500){
        $("#zuocedaohuang>ul").css("margin-top",scrollTop+"px");
    }
    if (scrollTop<500){
        $("#zuocedaohuang>ul").css("margin-top","0")
    }
})







var xingming=$("#profile div:nth-child(1) input");
var dianhua=$("#profile div:nth-child(2) input");
var dizhi=$("#profile div:nth-child(3) input");
var zhuhuleixing=$("#profile select");
var chongzhi=$("#profile .btn-chongzhi");
var shanchuyemian=1;

//存放所有用户
var shebei = shebei || {};

//操作类型
var operateType = "";
//存放搜索对象
var searchUsers = searchUsers || {};

var Shebei = {
    Create:function(xh,xm,dh,zz,zhlx){
        this.xh = xh;
        this.xm = xm;
        this.dianhua = dh;

        this.zz = zz;
        this.zhlx = zhlx;
    },
    //添加用户
    addUserData:function(){
        if(this.xh != ""){
            shebei[this.xh] = this;
        }
    },
}
function New(aClass,aParams){
    function new_(){
        aClass.Create.apply(this,aParams);
    }
    new_.prototype = aClass;
    return new new_();
}
//bootstrap模态框事件
$('#myModal').on('hide.bs.modal', function () {
    // 执行一些动作...
    var inputElements = this.getElementsByTagName("input");
    var userArr = [];
    for(var i=0;i<inputElements.length;i++){
        userArr[i] = inputElements[i].value;

        this.getElementsByTagName("input")[i].value="";
    }
    var user = New(Shebei,userArr);
    //添加操作
    if(operateType == "add"){
        user.addUserData();
        refreshDatas(shebei);
        operateType="";
        yemashu();
        fanye1();
        shanchuyemian=1;
        yemaheshuj();
    }

});
/**
 * 往表格添加一行html数据
 */
function addRowData(datas){
    var tbodyElement = document.getElementById("tbody");
    var html = "";
    for(var i in datas){
        html = html +  "<tr><th>"
            + datas[i].xh +"</th><th>"
            + datas[i].xm +"</th><th>"
            + datas[i].dianhua +"</th><th>"
            + datas[i].zz +"</th><th>"
            + datas[i].zhlx +"</th><th><button data-toggle=\"modal\" data-target=\"#myModal\" onclick='bianji(this)'>编辑</button> <button onclick='shanchu(this)'>删除</button></th></tr>";
    }
    tbodyElement.innerHTML = html;
}
/**
 * 刷新用户数据
 */

shuchu();
shuaxing(shebei);

function shuaxing(datas){
    if(shebei)
    {addRowData(datas);}
    operateType="";
    yemashu();
    fanye1();
    shanchuyemian=1;
    yemaheshuj();
}
function refreshDatas(datas){
    addRowData(datas);
}

//显示
function shuchu() {
        if(wulianshebeipanding==true){
            wulianshebeipanding=false;
            init();
            for (var i=0;i<wulianshebei.length;i++){
                var initUser=New(Shebei,wulianshebei[i]);
                shebei[initUser.xh] = initUser;
            }
            // return users;
            var userArray = shebei;
            addRowData(userArray);
            refreshDatas(shebei);
            // fanye1();
            yemashu();
            fanye1();
            shanchuyemian=1;
            yemaheshuj();
        }
}



//
// function initUserDatas(){
//     var initUser1 = New(User,["1","1号门","233","1号楼","智慧门禁"]);
//     var initUser2 = New(User,["2","2号门","854","2号楼","智慧门禁"]);
//     var initUser3 = New(User,["3","3号门","452","3号楼","智慧门禁"]);
//     var initUser4 = New(User,["4","4号门","842","4号楼","智慧门禁"]);
//     var initUser5 = New(User,["5","5号门","233","5号楼","智慧门禁"]);
//     var initUser6 = New(User,["6","6号门","854","6号楼","智慧门禁"]);
//     shebei[initUser1.xh] = initUser1;
//     shebei[initUser2.xh] = initUser2;
//     shebei[initUser3.xh] = initUser3;
//     shebei[initUser4.xh] = initUser4;
//     shebei[initUser5.xh] = initUser5;
//     shebei[initUser6.xh] = initUser6;
//     return shebei;
// }
/**
 * 首次加载页面执行方法
 */
// function loadUserDatas(){
//     var userArray = initUserDatas();
//     addRowData(userArray);
//     refreshDatas(shebei);
//     // fanye1();
//     yemashu();
//      fanye1();
// }
//用户操作
function add(param){
//获得操作类别
    document.getElementById("myModalLabel").innerText="添加用户";
    operateType = "add";

}

//删除
function shanchu(odj) {
    var i= parseInt($(odj).parent().parent().find("th")[0].innerText) ;
    delete shebei[i];
    $(odj).parent().parent().remove();
    yemashu();
    fanye2();

    yemaheshuj();
}

//编辑
function bianji(odj) {
    var i= parseInt($(odj).parent().parent().find("th")[0].innerText) ;
    var modal_body = document.getElementById("modal-body");
    var inputElements=  modal_body.getElementsByTagName("input");
    document.getElementById("myModalLabel").innerText="修改用户";
    inputElements[0].value=shebei[i].xh;
    inputElements[1].value=shebei[i].xm;
    inputElements[2].value=shebei[i].dianhua;
    inputElements[3].value=shebei[i].zz;
    inputElements[4].value=shebei[i].zhlx;
    operateType = "add";
}


//姓名查询
xingming.keyup(function () {
    if(xingming.val()){
        for(var i in shebei){
            if(shebei[i].xm.indexOf(xingming.val()) >= 0){
                searchUsers[shebei[i].xh] = shebei[i];
                refreshDatas(searchUsers);
            }
        }
    }else{
        for(var i in shebei){
            searchUsers[shebei[i].xh] = shebei[i];
            refreshDatas(searchUsers);
        }
    }
    searchUsers={};
    yemashu();
    fanye1();
    shanchuyemian=1;
    yemaheshuj();
})
//电话查询
dianhua.keyup(function () {
    if(dianhua.val()){
        for(var i in shebei){

            if(shebei[i].dianhua.indexOf(dianhua.val()) >= 0){
                searchUsers[shebei[i].xh] = shebei[i];
                refreshDatas(searchUsers);
            }
        }
    }else{
        for(var i in shebei){
            searchUsers[shebei[i].xh] = shebei[i];
            refreshDatas(searchUsers);
        }
    }
    searchUsers={};
    yemashu();
    fanye1();
    shanchuyemian=1;
    yemaheshuj();
})
//地址查询
dizhi.keyup(function () {
    if(dizhi.val()){
        for(var i in shebei){
            if(shebei[i].zz.indexOf(dizhi.val()) >= 0){
                searchUsers[shebei[i].xh] = shebei[i];
                refreshDatas(searchUsers);
            }
        }
    }else{
        for(var i in shebei){
            searchUsers[shebei[i].xh] = shebei[i];
            refreshDatas(searchUsers);
        }
    }
    searchUsers={};
    yemashu();
    fanye1();
    shanchuyemian=1;
    yemaheshuj();
})
//住户类型查询
zhuhuleixing.keyup(function () {
    if(zhuhuleixing.val()!="全部"){
        for(var i in shebei){
            if(shebei[i].zhlx.indexOf(zhuhuleixing.val()) >= 0){
                searchUsers[shebei[i].xh] = shebei[i];
                refreshDatas(searchUsers);
            }
        }
    }else{
        for(var i in shebei){
            searchUsers[shebei[i].xh] = shebei[i];
            refreshDatas(searchUsers);
        }
    }
    searchUsers={};
    yemashu();
    fanye1();
    shanchuyemian=1;
    yemaheshuj();
})
//重置
// chongzhi.click(function () {
//     for(var i in shebei){
//         searchUsers[shebei[i].xh] = shebei[i];
//         refreshDatas(searchUsers);
//     }
//     yemashu();
//     fanye1();
//     shanchuyemian=1;
//     yemaheshuj();
// })
//页面数
function yemashu() {
    var yema=$(".section1 .yema");
    var neirong=$("#profile #tbody tr");
    var yema1=Math.ceil(neirong.length/8);
    var html="";
    if(yema1>0){
        for (var i=1;i<=yema1;i++){
            html=html+"<button onclick='fanye(this)'>"+i+"</button>";
        }
        yema[0].innerHTML=html;
        html="";
    }
}
//翻页
function fanye(odj) {
    var neirong=$("#profile #tbody tr");
    neirong.css("display","none");
    shanchuyemian=odj.innerText;
    for(var i=0;i<neirong.length;i++){
        if(i>=odj.innerText*8-8&&i<odj.innerText*8){
            if(i==neirong.length)break;
            neirong[i].style.display="table-row";
        }
    }
    yemaheshuj();
}
//加载翻页
function fanye1() {
    var neirong=$("#profile #tbody tr");

    neirong.css("display","none");
    for(var i=0;i<8;i++){
        if(i>=0&&i<8){

            if(i==neirong.length)break;
            neirong[i].style.display="table-row";
        }
    }
}
function fanye2(){
    var neirong=$("#profile #tbody tr");
    neirong.css("display","none");
    for(var i=0;i<neirong.length;i++){
        if(i>=shanchuyemian*8-8&&i<shanchuyemian*8){
            if(i==neirong.length)break;
            neirong[i].style.display="table-row";
        }
    }
}
function yemaheshuj() {
    var yemaheshujun=$(" .yemaheshujun");
    var neirong=$("#profile #tbody tr");
    yemaheshujun.text("当前页面："+shanchuyemian+" 一共"+neirong.length+"条数据")
}





